package com.cool.controller.sysmanagecontroller;

import com.cool.bo.sysmanage.common.PageQueryBo;
import com.cool.bo.sysmanage.resource.PermissionQueryBo;
import com.cool.common.CommonResult;
import com.cool.common.ResponseCode;
import com.cool.po.sysmanagepo.PermissionPo;
import com.cool.service.sysmanage.PermissionService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 权限控制器
 *
 * @author Jiangmanman
 * @date 2020/07/29
 */
@RequestMapping("/resource")
@ResponseBody
@Controller
public class PermissionController {

    /**
     * 许可服务
     *
     * @date 2020/07/30
     * @see PermissionService
     */
    @Resource
    private PermissionService permissionService;

    /**
     * 分页查询
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/29
     */
    @GetMapping("/list")
    public CommonResult pagingQuery(PermissionQueryBo permissionQueryBo){

        System.out.println("permissionQueryBo : "+permissionQueryBo);

        PageQueryBo pageQueryBo = permissionService.pagingQuery(permissionQueryBo);

        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询成功").setData(pageQueryBo);
    }

    /**
     * 创建
     *
     * @param permissionPo 权限
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/create")
    public CommonResult create(@RequestBody PermissionPo permissionPo){
        System.out.println("permissionPo = "+permissionPo);
        Integer len = permissionService.insert(permissionPo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.PERMISSION_INSERT_FAILURE).setMsg("更新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 删除
     *
     * @param id id
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/delete/{id}")
    public CommonResult delete(@PathVariable("id")Integer id){
        Integer len = permissionService.deleteById(id);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.PERMISSION_DELETE_FAILURE).setMsg("删除失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 更新
     *
     * @param id           id
     * @param permissionPo 资源
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @PostMapping("/update/{id}")
    public CommonResult update(@PathVariable("id")Integer id,@RequestBody PermissionPo permissionPo){
        Integer len = permissionService.updateById(id,permissionPo);
        if(len < 1){
            return CommonResult.newCommonResult().setCode(ResponseCode.PERMISSION_UPDATE_FAILURE).setMsg("更新失败");
        }else {
            return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("更新成功");
        }
    }

    /**
     * 列出所有
     *
     * @return {@link CommonResult }
     * @author Jiangmanman
     * @date 2020/07/30
     */
    @GetMapping("/listAll")
    public CommonResult listAll(){

        List<PermissionPo> list = permissionService.pagingQueryAll();

        return CommonResult.newCommonResult().setCode(ResponseCode.USER_LOGIN_SUCCESS).setMsg("查询全部成功").setData(list);
    }

}
